package com.zzyl.nursing.controller;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.dto.CancelDto;
import com.zzyl.nursing.dto.DoPlanDto;
import com.zzyl.nursing.dto.UpdateJobDto;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.nursing.vo.NursingTaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;

import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 护理任务Controller
 *
 * @author ruoyi
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/nursing/task")
@Api(tags = "护理任务相关接口")
public class NursingTaskController extends BaseController {
    @Autowired
    private INursingTaskService nursingTaskService;

    @PutMapping("/updateTime")
    public AjaxResult updateTime(@RequestBody UpdateJobDto updateJobDto) {
        nursingTaskService.updateTime(updateJobDto);
        return AjaxResult.success();
    }

    @PutMapping("/do")
    public AjaxResult doPlan(@RequestBody DoPlanDto dto) {
        nursingTaskService.doPlan(dto);
        return AjaxResult.success();

    }

    /**
     * 2.2.3 取消任务
     *
     * @param cancelDto
     * @return
     */
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody CancelDto cancelDto) {
        if (ObjectUtil.isEmpty(cancelDto)) {
            throw new BaseException("参数为空");
        }
        boolean result = nursingTaskService.updateByTaskId(cancelDto);
        return result ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 2.2.2 获取护理任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取护理任务详细信息")
    public R<NursingTaskVO> getInfo(@ApiParam(value = "护理任务ID", required = true)
                                    @PathVariable("id") Long id) {
        NursingTaskVO nursingTaskVO = nursingTaskService.selectNursingTaskById(id);
        return R.ok(nursingTaskVO);
    }

    /**
     * 2.2.1 查询护理任务列表
     */
//    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取护理任务列表")
    public TableDataInfo<List<NursingTask>> list(NursingTask nursingTask) {
        startPage();
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
        return getDataTable(list);
    }


// 以上为我写的---------------------------------------------------------------------------


    /**
     * 导出护理任务列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:export')")
    @Log(title = "护理任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出护理任务列表")
    public void export(HttpServletResponse response, NursingTask nursingTask) {
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
        ExcelUtil<NursingTask> util = new ExcelUtil<NursingTask>(NursingTask.class);
        util.exportExcel(response, list, "护理任务数据");
    }


    /**
     * 新增护理任务
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:add')")
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增护理任务")
    public AjaxResult add(@ApiParam(value = "护理任务实体", required = true) @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 修改护理任务
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:edit')")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改护理任务")
    public AjaxResult edit(@ApiParam(value = "护理任务实体", required = true) @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }

    /**
     * 删除护理任务
     */
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:remove')")
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除护理任务")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids) {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }


}
